package com.ysr.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.ysr.pojo.PayMent;
import com.ysr.result.CommonResult;
import com.ysr.service.PayMentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "consumer")
@Slf4j
public class OrderFeignController {

    @Autowired
    private PayMentService paymentService;
    //创建支付订单的接口
    @PostMapping("/payment/create")
    public CommonResult<PayMent> create(PayMent payMent){
       CommonResult result = paymentService.create(payMent);
       return result;
    }
    //获取id获取支付订单
    @GetMapping("/payment/get/{id}")
    public CommonResult<PayMent> getPayment(@PathVariable("id") Long id){

        System.out.println(id);
        CommonResult result = paymentService.queryById(id);
        return result;
    }

    //创建支付订单的接口
    @PostMapping("/payment/add")
    public CommonResult<PayMent> add(PayMent payMent){
        CommonResult result = paymentService.add(payMent);
        return result;
    }

    //获取id获取支付订单
    @GetMapping("/payment/queryPayMentById")
    public CommonResult<PayMent> queryPayMentById(Long id){
        CommonResult result = paymentService.queryPayMentById(id);
        return result;
    }
    @GetMapping("/feign/timeout")
    public String PaymentFeignTimeOut() throws InterruptedException{
        return paymentService.PaymentFeignTimeOut();
    }

    @GetMapping("/hystrix/ok/{id}")
    String paymentInfo_OK(@PathVariable("id") Integer id){
        return  paymentService.paymentInfo_OK(id);
    }


    @GetMapping("/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod",commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1000")
    })
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
       String str = paymentService.paymentInfo_TimeOut(id);
        System.out.println(str);
        return str;
    }
    //服务降级的兜底的方法
    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id) {
        return "我是消费者80,对方支付系统繁忙请10秒种后再试或者自己运行出错请检查自己,o(╥﹏╥)o";
    }
}
